package com.haruhi.bot.endpoint.ws.model;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.websocket.Session;
import java.io.IOException;
import java.net.URI;

public class ProxySession {

    private WebSocketSession webSocketSession;
    private Session session;
    public ProxySession(WebSocketSession webSocketSession){
        this.webSocketSession = webSocketSession;
    }

    public ProxySession(Session session){
        this.session = session;
    }

    public void sendMessage(String message) throws IOException {
        if(webSocketSession != null){
            webSocketSession.sendMessage(new TextMessage(message));
        }else if(session != null){
            session.getAsyncRemote().sendText(message);
        }
    }

    public boolean isOpen(){
        if(webSocketSession != null){
            return webSocketSession.isOpen();
        }else if(session != null){
            return session.isOpen();
        }
        return false;
    }

    public String getId(){
        if(webSocketSession != null){
            return webSocketSession.getId();
        }else if(session != null){
            return session.getId();
        }
        return null;
    }

    public void close() throws IOException {
        if(webSocketSession != null){
            webSocketSession.close();
        }else if(session != null){
            session.close();
        }
    }

    public URI getUri(){
        if(webSocketSession != null){
            return webSocketSession.getUri();
        }else if(session != null){
            return session.getRequestURI();
        }
        return null;
    }
}
